package vip.xiaonuo.bi.core.datasource.util;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.ibatis.jdbc.SQL;
import vip.xiaonuo.bi.modular.dataset.enums.DateFunc;
import vip.xiaonuo.bi.modular.dataset.enums.DorisCanstant;

import java.util.List;

/**
 * @auther: 顾育司
 * @Description 用途: 动态生成sql语句工具类
 * @date: 2024/1/8
 */
public class DynamicSqlUtils {

    public static <T> String buildSql(String select, String where, String tableName, String groupBy, String orderBy, int offset, int limit) {
        SQL sql = new SQL();
        sql.SELECT(select==null|| StringUtils.isBlank(select.trim())?"*":select)
                .FROM(tableName);
        if (ObjectUtils.isNotEmpty(groupBy)) {
            sql.GROUP_BY(groupBy);
        }
        if (ObjectUtils.isNotEmpty(where)) {
            sql.WHERE(where);
        }
        if (ObjectUtils.isNotEmpty(orderBy)) {
            sql.ORDER_BY(orderBy);
        }
        if (ObjectUtils.isNotEmpty(offset)) {
            sql.OFFSET(offset);
        }
        if (ObjectUtils.isNotEmpty(limit)) {
            sql.LIMIT(limit);
        }
        return sql.toString();
    }




    /**
     * 动态过滤算法拼成where条件部分过滤
     * @param filters
     * @return
     */
    public static String getWhere(List<List<Filter>> filters) {
        StringBuilder sql = new StringBuilder();
        for (List<Filter> sublist : filters) {
            String condition = String.join(" and ", sublist.stream()
                    .map(filter -> {
                        if("1".equals(filter.getMatchType())){
                            return filter.getFilterField() + " " + DorisCanstant.getValue(filter.method)  + " '" + filter.getMatchValue() + "'";
                        }
                        if("2".contains(filter.getMatchType())){
                            return filter.getFilterField() + " " + DorisCanstant.getValue(filter.method)  + " `" + filter.getMatchValue() + "`";
                        }
                        if("3".contains(filter.getMatchType())){
                            return filter.getFilterField() + " " + DorisCanstant.getValue(filter.method)  + " '" + filter.getMatchValue() + "'";
                        }
                        if("4".equals(filter.getMatchType())){
                            DateFunc date = DateFunc.getType(filter.getMatchValue());
                            String entTime = date.entTime();
                            String startTime = date.startTime();
                            //to_date("2020-02-02 00:00:00")
                            return filter.getFilterField() + " >=" +  " '" + startTime + "'" + " and "+ filter.getFilterField() + " <" +  " '" + entTime + "'";
                        }
                        return "";
                    })
                    .toArray(String[]::new));
            sql.append("(").append(condition).append(") or ");
        }
        return sql.toString().replaceAll(" or $", ""); //移除最后的"or"
    }

    @Builder
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Filter{
        private String filterField;
        private String method;
        private String matchValue;
        private String matchType; // 1.定义值 2.填充字段 3.定义时间 4.动态时间
    }

}
